import 'package:flutter/material.dart';

import 'HttpConfig/http.dart';

class NewListPage extends StatefulWidget {
  NewListPage({Key key}) : super(key: key);

  @override
  _NewListPageState createState() => _NewListPageState();
}

class _NewListPageState extends State<NewListPage> {
  ScrollController _scrollController = ScrollController();
  List listData = new List();
  int page = 1;
  bool isLoading = false;
  bool noMore = false;

  void _getDataList(index) async {
    setState(() {
      isLoading = true;
    });
    Map result = await ResustHttp.getNewListData(index);
    print(result);

    if(index == 1){
      print("刷新");
      //刷新的情况
      setState(() {
       this.listData = result["result"];
       this.page++;
       isLoading = false;
      });
      print(this.listData);
    }else{
      setState(() {
       this.listData.addAll(result["result"]);
       this.page++;
       isLoading = false;
      });
    }
    result["result"].length < 20 ? setState(() {
        this.noMore = true;
    }) : setState(() {
        this.noMore = false;
    });

  }

  @override
  void initState() {
    super.initState();
    this._getDataList(1);
    _scrollController.addListener(() {
      if (_scrollController.position.pixels >
          _scrollController.position.maxScrollExtent - 10 && !this.isLoading && !this.noMore) {
        print('滑动到了最底部');
        _getDataList(this.page);
      }
    });
  }

  Future<void> _onRefresh() async {
    setState(() {
      page = 1;
    });
    this._getDataList(1);
  }

  Widget renderBottom() {
    if(!this.noMore){
      if(this.isLoading) {
        return Container(
          padding: EdgeInsets.symmetric(vertical: 20),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(
                '努力加载中...',
                style: TextStyle(
                  fontSize: 15,
                  color: Color(0xFF333333),
                ),
              ),
              Padding(padding: EdgeInsets.only(left: 10)),
              SizedBox(
                width: 20,
                height: 20,
                child: CircularProgressIndicator(strokeWidth: 3),
              ),
            ],
          ),
        );
      } else {
        return Container(
          padding: EdgeInsets.symmetric(vertical: 15),
          alignment: Alignment.center,
          child: Text(
            '上拉加载更多',
            style: TextStyle(
              fontSize: 15,
              color: Color(0xFF333333),
            ),
          ),
        );
      }
    }else{
      return Container(
          padding: EdgeInsets.symmetric(vertical: 15),
          alignment: Alignment.center,
          child: Text(
            '-我是有底线的-',
            style: TextStyle(
              fontSize: 15,
              color: Color(0xFF333333),
            ),
          ),
        );
    }
  }
  Widget _itemBuilder(itemdata) {
    return ListTile(
      title: Text(
        itemdata["title"],
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
      ),
      subtitle: Text(itemdata["username"]),
      onTap: (){
        Navigator.pushNamed(context, "/newdetails",arguments: {
          "aid":itemdata["aid"]
        });
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("新闻列表"),
        ),
        body: listData.isNotEmpty
            ? RefreshIndicator(
                child: ListView.separated(
                  controller: this._scrollController,
                  itemCount: listData.length + 1,
                  itemBuilder: (context, index) {
                     if(index < listData.length ){
                       return _itemBuilder(listData[index]);
                     }else{
                       //在最后一条数据后面加上一个 加载的图标
                       return renderBottom();
                     }
                  },
                  separatorBuilder: (context, index) {
                    return Divider();
                  },
                ),
                onRefresh: _onRefresh,
              )
            : Center(
                child: CircularProgressIndicator(
                  semanticsLabel: "加载中...",
                ),
              ));
  }
}
